// 代码块 importM
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: const Text("Flutter app"),
        ),
        body: MyHomePage(),
      ),
    );
  }
}

List listData = [
  {
    "title": 'Candy Shop',
    "author": 'Mohamed Chahin',
    "imageUrl":
        "https://tse3-mm.cn.bing.net/th/id/OIP-C.FYx55PIoNajL-lEV5UhFlAHaNK?rs=1&pid=ImgDetMain",
  },
  {
    "title": 'Candy Shop2',
    "author": 'Mohamed Chahin',
    "imageUrl":
        "https://tse3-mm.cn.bing.net/th/id/OIP-C.FYx55PIoNajL-lEV5UhFlAHaNK?rs=1&pid=ImgDetMain",
  },
  {
    "title": 'Candy Shop3',
    "author": 'Mohamed Chahin',
    "imageUrl":
        "https://tse3-mm.cn.bing.net/th/id/OIP-C.FYx55PIoNajL-lEV5UhFlAHaNK?rs=1&pid=ImgDetMain",
  },
  {
    "title": 'Candy Shop4',
    "author": 'Mohamed Chahin',
    "imageUrl":
        "https://tse3-mm.cn.bing.net/th/id/OIP-C.FYx55PIoNajL-lEV5UhFlAHaNK?rs=1&pid=ImgDetMain",
  },
];

class MyHomePage extends StatelessWidget {
  List<String> list = [];
  MyHomePage({Key? key}) : super(key: key);

  Widget _initGridViewData(context, index) {
    return Container(
          decoration: BoxDecoration(border: Border.all(color: Colors.black26)),
          child: Column(
            children: [
              Image.network(listData[index]['imageUrl']),
              const SizedBox(
                height: 10,
              ),
              Text(
                listData[index]["title"],
                style: const TextStyle(fontSize: 18),
              ),
            ],
          ));
  }

  @override
  Widget build(BuildContext context) {
    return GridView.builder(
      padding: const EdgeInsets.all(10),
      gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 2, //横轴子元素的最大长度
        crossAxisSpacing: 10, // 水平间距
        mainAxisSpacing: 10, // 垂直间距
        childAspectRatio: 0.5,
      ), // 宽高比)
      itemBuilder: _initGridViewData,
      itemCount: listData.length, // 列表的数量，不传会出现死循环
    );
  }
}
